package com.tangyee.home_exercise_diet.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.tangyee.home_blood_center.utils.DateUtil;
import com.tangyee.home_blood_center.utils.DateUtil;
import com.tangyee.home_exercise_diet.beans.FoodOrExercise;
import com.tangyee.home_exercise_diet.beans.TypeEnum;
import com.tangyee.home_exercise_diet.dao.FoodOrExerciseDbSchema.FoodOrExerciseTable;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class FoodOrExerciseHolder {

    private static FoodOrExerciseHolder sFoodOrExerciseHolder;
    private Context mContext;
    private SQLiteDatabase mDatabase;

    /**
     * 获取本类实例 @param context @return
     */
    public static FoodOrExerciseHolder get(Context context) {
        if (sFoodOrExerciseHolder == null) sFoodOrExerciseHolder = new FoodOrExerciseHolder(context);
        return sFoodOrExerciseHolder;
    }

    /**
     * 私有化构造方法，借用整个应用的上下文将本类设计成单例 @param context
     */
    private FoodOrExerciseHolder(Context context) {
        mContext = context.getApplicationContext();
        mDatabase = new FoodOrExerciseDbHelper(mContext).getWritableDatabase();
    }

    /**
     * 通过日期获取记录
     * @param date
     * @return
     */
    public List<FoodOrExercise> getListByDate(Date date){
        String d = DateUtil.date2Str(date, "yyyy-MM-dd");
        List<FoodOrExercise> foodOrExerciseList = new ArrayList<>();
        FoodOrExerciseDbCursorWrapper cursorWrapper = queryRecords(FoodOrExerciseDbSchema.FoodOrExerciseTable.Cloumns.DATE + "=?", new String[]{d});
        try{
            cursorWrapper.moveToFirst();
            while(!cursorWrapper.isAfterLast()){
                foodOrExerciseList.add(cursorWrapper.getFoodOrExerciseRecord());
                cursorWrapper.moveToNext();
            }
        } finally {
            cursorWrapper.close();
        }
        return foodOrExerciseList;
    }

    /**
     * 通过日期和记录类型获取相应的记录
     * @param date
     * @param type
     * @return
     */
    public List<FoodOrExercise> getListByDateAndType(String  date, String type){
        List<FoodOrExercise> foodOrExerciseList = new ArrayList<>();
        FoodOrExerciseDbCursorWrapper cursorWrapper = queryRecords(
                FoodOrExerciseDbSchema.FoodOrExerciseTable.Cloumns.DATE + "=?" + " and " +
                FoodOrExerciseDbSchema.FoodOrExerciseTable.Cloumns.TYPE + "=?", new String[]{date,type}
                );
        try{
            cursorWrapper.moveToFirst();
            while(!cursorWrapper.isAfterLast()){
                foodOrExerciseList.add(cursorWrapper.getFoodOrExerciseRecord());
                cursorWrapper.moveToNext();
            }
        } finally {
            cursorWrapper.close();
        }
        if (foodOrExerciseList == null)
            foodOrExerciseList = new ArrayList<>();
        return foodOrExerciseList;
    }

    /**
     * 获得收藏的记录
     * @param typeEnum
     * @return
     */
    public List<FoodOrExercise> getCollects(TypeEnum typeEnum){
        List<FoodOrExercise> foodOrExerciseList = new ArrayList<>();
        FoodOrExerciseDbCursorWrapper cursorWrapper = queryRecords(
                FoodOrExerciseDbSchema.FoodOrExerciseTable.Cloumns.DATE + "=?" + " and " +
                        FoodOrExerciseDbSchema.FoodOrExerciseTable.Cloumns.TYPE + "=?",
                new String[]{"1111-11-11",typeEnum.name()}
        );
        try{
            cursorWrapper.moveToFirst();
            while(!cursorWrapper.isAfterLast()){
                foodOrExerciseList.add(cursorWrapper.getFoodOrExerciseRecord());
                cursorWrapper.moveToNext();
            }
        } finally {
            cursorWrapper.close();
        }
        if (foodOrExerciseList == null)
            foodOrExerciseList = new ArrayList<>();
        return foodOrExerciseList;
    }

    /**
     * 根据条件查询数据库
     * @param whereClause
     * @param whereArgs
     * @return
     */
    private FoodOrExerciseDbCursorWrapper queryRecords(String whereClause, String[] whereArgs) {
        Cursor cursor = mDatabase.query(FoodOrExerciseDbSchema.FoodOrExerciseTable.NAME, null, whereClause, whereArgs, null, null, null);
        return new FoodOrExerciseDbCursorWrapper(cursor);
    }

    /**
     * 将bean转换成ContentValues
     * @param foodOrExercise
     * @return
     */
    private static ContentValues getContentValues(FoodOrExercise foodOrExercise) {
        ContentValues values = new ContentValues();

        values.put(FoodOrExerciseDbSchema.FoodOrExerciseTable.Cloumns.DATE, foodOrExercise.getDate());
        values.put(FoodOrExerciseDbSchema.FoodOrExerciseTable.Cloumns.TYPE, foodOrExercise.getType().name());
        values.put(FoodOrExerciseDbSchema.FoodOrExerciseTable.Cloumns.NAME, foodOrExercise.getName());
        values.put(FoodOrExerciseDbSchema.FoodOrExerciseTable.Cloumns.PER_UNIT_CALORIE, foodOrExercise.getPerUnitCalorie());
        values.put(FoodOrExerciseDbSchema.FoodOrExerciseTable.Cloumns.UNIT, foodOrExercise.getUnit());
        values.put(FoodOrExerciseDbSchema.FoodOrExerciseTable.Cloumns.AMOUNT, foodOrExercise.getAmount());
        values.put(FoodOrExerciseDbSchema.FoodOrExerciseTable.Cloumns.IMAGE, foodOrExercise.getImg());

        return values;
    }

    /**
     * 获得记录总数
     * @return
     */
    public int getRecordsCount() {
        int count = 0;
        FoodOrExerciseDbCursorWrapper cursor = queryRecords(null, null);
        count = cursor.getRecordsCount();
        cursor.close();
        return count;
    }

    /**
     * 插入一条记录
     * @param foodOrExercise
     */
    public void insertRecord(FoodOrExercise foodOrExercise){
        ContentValues contentValues = getContentValues(foodOrExercise);
        mDatabase.insert(FoodOrExerciseDbSchema.FoodOrExerciseTable.NAME,null, contentValues);
    }

    /**
     * 更新一条记录
     * @param foodOrExercise
     */
    public void updateRecord(FoodOrExercise foodOrExercise) {
        ContentValues contentValues = getContentValues(foodOrExercise);
        int r = mDatabase.update(FoodOrExerciseDbSchema.FoodOrExerciseTable.NAME, contentValues,
                FoodOrExerciseDbSchema.FoodOrExerciseTable.Cloumns.DATE + "=?" +
                        " and " + FoodOrExerciseDbSchema.FoodOrExerciseTable.Cloumns.TYPE + "=?" +
                        " and " + FoodOrExerciseDbSchema.FoodOrExerciseTable.Cloumns.NAME + "=?",
                new String[]{foodOrExercise.getDate(), foodOrExercise.getType().name(), foodOrExercise.getName()});
    }

    /**
     * 删除一条记录
     * @param date
     * @param type
     * @param name
     */
    public void deleteRecord(Date date, TypeEnum type, String name){
        mDatabase.delete(FoodOrExerciseDbSchema.FoodOrExerciseTable.NAME,
                FoodOrExerciseDbSchema.FoodOrExerciseTable.Cloumns.DATE + "=?" +
                        " and " + FoodOrExerciseDbSchema.FoodOrExerciseTable.Cloumns.TYPE + "=?" +
                        " and " + FoodOrExerciseDbSchema.FoodOrExerciseTable.Cloumns.NAME + "=?",
                new String[]{DateUtil.date2Str(date, "yyyy-MM-dd"), type.name(), name});
    }
    public void deleteRecord(String date, String type, String name){
        mDatabase.delete(FoodOrExerciseDbSchema.FoodOrExerciseTable.NAME,
                FoodOrExerciseDbSchema.FoodOrExerciseTable.Cloumns.DATE + "=?" +
                        " and " + FoodOrExerciseDbSchema.FoodOrExerciseTable.Cloumns.TYPE + "=?" +
                        " and " + FoodOrExerciseDbSchema.FoodOrExerciseTable.Cloumns.NAME + "=?",
                new String[]{date, type, name});
    }

}
